
package com.the9.C9Alarm;



import mainView.boxGame;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

public class AlarmReceiveActivity extends Activity {
    Context c;

  //  AlarmBean alm;

    int index;
    Intent nextIntent;
    ImageButton ib_awake;
    ImageButton ib_sleep;
    ImageButton ib_anim;
    NotificationManager nfManger;
    Notification mNotification;
    Thread autoOff;
    AlarmBean ab;
    boolean isOver = false;
    AnimationDrawable fm;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE );
        this.setContentView(R.layout.alert_alarm);
        Intent intentGet = this.getIntent();
       

        autoOff = new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(60000);
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(isOver==false)
					   stillSleep();
				}
				
			}
        	
        });
        autoOff.start();
        
        nfManger = (NotificationManager)this.getSystemService(Activity.NOTIFICATION_SERVICE);
      
	       
        PendingIntent pt= PendingIntent.getActivity(this,0, intentGet, 0);
        mNotification= new Notification();
        mNotification.icon=R.drawable.ic_clock_add_alarm;
        mNotification.tickerText="闹钟娘";
        mNotification.defaults=Notification.DEFAULT_SOUND;
        mNotification.setLatestEventInfo(this, "闹钟", "test", pt);
       
        mNotification.flags |= mNotification.FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中 
        mNotification.flags |= mNotification.FLAG_NO_CLEAR; 
        nfManger.notify(Symbol.NOTIFICATION_RINGING,mNotification);
       
        
        c = AlarmReceiveActivity.this;
        //添加或生成一次新的计时
        Utils.addorStartSleepTime(c);
       // AlarmBean[]abx= AlarmProvider.geInstance(c).getAlarms();
        AlarmProvider.geInstance(c).disableSleepAlarm(0);
        
        ib_awake = (ImageButton) findViewById(R.id.imageButton1);
        ib_sleep = (ImageButton) findViewById(R.id.imageButton2);
        ib_anim = (ImageButton) findViewById(R.id.imageView1);
  //      ib_anim.setBackgroundResource(R.anim.of_native_loader_progress);
         fm =(AnimationDrawable) ib_anim.getDrawable();
       // fm.selectDrawable(R.drawable.of_native_loader_progress);
        //ib_anim.setAnimation(fm);
     //   fm.start();
        
       // alm = (AlarmBean) intentGet.getSerializableExtra("alarm");// .getBundleExtra("alarm");
        index = intentGet.getIntExtra("alarm_index", -1);
        String path;
        ab = AlarmProvider.geInstance(c).getAlarm(index);
        Form f= Form.getForm(ab.getForm());
        if(f!=null){
        	
        	 Media.getInstance(c).playMusic(f.getAlarmMusic(), AlarmProvider.geInstance(c).getAlarm(index).getVibrate() );  

        }else{
        	path = ab.getUri();
        	 Media.getInstance(c).playMusic(path, AlarmProvider.geInstance(c).getAlarm(index).getVibrate() );  

        }
       
        ib_awake.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tryToAwake();
				// awake();
            	
			}});
        		
        ib_sleep.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				stillSleep();
                 
			}});
        
                     
    }
   
    private  synchronized void tryToAwake(){
		isOver = true;
		Utils.GameStart();
		if (ab.getEnableGame() == true) {
			Intent i = new Intent(this, boxGame.class);
			this.startActivity(i);
		} else {
			Intent intent = new Intent(Symbol.ACTION_WIN);
			sendBroadcast(intent);
		}
		this.finish();
    }
    public void onWindowFocusChanged(boolean hasFocus) {
    	// TODO Auto-generated method stub
    	super.onWindowFocusChanged(hasFocus);
    	if(hasFocus)
    		fm.start();
    	else
    		fm.stop();
    	}

    private synchronized void stillSleep(){
    	 AlarmBean ab = AlarmProvider.geInstance(c).getAlarm(index);
         ab.setDay(Symbol.DAY_ONCE);// sym
         ab.setInterval(ab.getSleepDelay() * 1000 * 60l);// sym
         ab.setTime(System.currentTimeMillis() + ab.getInterval());
         ab.setIndex(0);//sleepalarm ,start from 0 
        // ab.setName("10fenyan");
         ab.setEnabled(true);

       //  AlarmProvider.geInstance(c).setSleepAlarm(ab);
         nameIsToHard();
    }
    
     private synchronized void  nameIsToHard(){
    	  Utils.GameReset();
    	  isOver=true;
    	  nfManger.cancel(0);
    	  AlarmSet.setAlarm(this);
        
          
          Media.getInstance(c).Stop();
          Intent i = new Intent();
          i.setAction(Symbol.ACTION_DONOTHING);
          i.setClass(c, AlarmReceiver.class);
        
          
          if(nextIntent!=null){
          	c.startActivity(nextIntent);
          }
          
          AlarmReceiveActivity.this.finish();
    }
}
